package inutan.common.config.datasource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.ricks.common.conf.RicksPropertiesLoader;
import org.ricks.ioc.anno.Bean;
import org.ricks.ioc.anno.Configuration;
import org.ricks.ioc.anno.Value;
import org.ricks.orm.ConnectionSourceHelper;
import org.ricks.orm.DataAccessor;
import org.ricks.orm.MysqlDataAccessor;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2022/12/714:33
 */
//@Configuration
public class DataSourceConfig {

    @Value("jdbc.url")
    public String url;

    @Bean("dataAccessor")
    public DataAccessor initDataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(RicksPropertiesLoader.get("jdbc.url"));//mysql
        hikariConfig.setDriverClassName(RicksPropertiesLoader.get("jdbc.driverClassName"));
        hikariConfig.setUsername(RicksPropertiesLoader.get("jdbc.username"));
        hikariConfig.setPassword(RicksPropertiesLoader.get("jdbc.password"));
        hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
//            hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setIdleTimeout(5000);
        hikariConfig.setMaximumPoolSize(30);
        hikariConfig.setMinimumIdle(10);
        hikariConfig.setKeepaliveTime(5000);
        hikariConfig.setValidationTimeout(3000);
        hikariConfig.setMaxLifetime(1000);
        hikariConfig.setRegisterMbeans(true);
        hikariConfig.setAllowPoolSuspension(false);
        HikariDataSource masterDataSource = new HikariDataSource(hikariConfig);
        return new MysqlDataAccessor(ConnectionSourceHelper.getSingle(masterDataSource));
    }

}
